package com.woniuxy.controller;

import com.woniuxy.dao.RefuelingRecordMapper;
import com.woniuxy.pojo.CurrentUnit;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.pojo.RefuelingRecord;
import com.woniuxy.service.RefuelingRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.RefuelingRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RefuelingRecordController extends BaseController{
    @Autowired
    private RefuelingRecordService refuelingRecordService;
    @Autowired
    private RefuelingRecordMapper refuelingRecordMapper;
    @GetMapping(value = "/refuelingRecords")
    public DataGridView selectDrvingRecordsByPage(RefuelingRecordVo refuelingRecordVo) {
        return refuelingRecordService.selectRefuelingRecordsByProperty(refuelingRecordVo);
    }
    /**
     * 根据id删除加油记录
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/refuelingRecords/{id}")
    public ResultDto deleteRefuelingRecord(@PathVariable("id") int id) {
        try {
            this.refuelingRecordService.deleteRefuelingRecordById(id);
            return ResultDto.DELETE_REF_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_REF_FAILURE;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/refuelingRecords/deleteBathRefuelingRecord")
    public ResultDto removeBatchRefuelingRecord(@RequestParam("ids[]") Integer[] ids) {
        try {
            this.refuelingRecordService.removeBatchRefuelingRecord(ids);
            return ResultDto.DELETE_REF_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_REF_FAILURE;
        }
    }
    @GetMapping("/refuelingRecords/currentUnits.do")
    public List<CurrentUnit> currentUnits() {
        return refuelingRecordMapper.selectAllStation();
    }
    @GetMapping("/refuelingRecords/oils.do")
    public List<Dictionary> oils() {
        return refuelingRecordMapper.selectAllOil();
    }
    @PostMapping(value = "/refuelingRecords.do")
    public ResultDto addRefuelingRecord(RefuelingRecord refuelingRecord) {
        boolean flag = this.refuelingRecordService.insertDrvingRecord(refuelingRecord);
        if (flag) {
            return ResultDto.REF_ADD_SUCCESS;
        } else {
            return ResultDto.REF_ADD_FAIL;
        }
    }
    @PostMapping(value = "/refuelingRecords/update.do")
    public ResultDto updateDrvingRecord(RefuelingRecord refuelingRecord){
        boolean flag = this.refuelingRecordService.updataDrvingRecord(refuelingRecord);
        if (flag){
            return ResultDto.REF_UPDATE_SUCCESS;
        }else {
            return ResultDto.REF_UPDATE_FAIL;
        }
    }

}
